package com.jianxiu.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.jianxiu.bean.Cons;

/**
 * Created by tancw on 2017/1/24.
 */
public class BaseInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        request.setCharacterEncoding(Cons.UTF_8);
        response.setCharacterEncoding(Cons.UTF_8);
        String path = ((HttpServletRequest) request).getContextPath();
        request.setAttribute("path", path);
        HttpServletRequest req = (HttpServletRequest) request;
        String request_url = req.getRequestURL().toString();
        String queryString = req.getQueryString();
        String url = StringUtils.isEmpty(queryString) ? request_url : request_url.concat("?").concat(queryString);
        request.setAttribute("request_url", url);
//        if (request.getSession().getAttribute(Cons.SESSION_USER) == null) {
//            request.setAttribute("appId", SysConf.get("weixin.appID"));
//            response.sendRedirect("tologin.do");
//            return false;
//        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response,
                                Object handler, Exception ex) throws Exception {

    }
}
